package com.gotop.auth.granter;

import com.gotop.common.enums.EnumTokenGranter;
import com.gotop.common.enums.EnumUser;
import com.gotop.auth.service.IUserService;
import com.gotop.common.utils.DigestUtil;
import com.gotop.common.utils.auth.TokenParameter;
import com.gotop.info.UserInfo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: cem
 * @description:
 * @author: superfish
 * @create: 2020-12-17 15:57
 **/
@Component
@AllArgsConstructor
public class PasswordTokenGranter implements ITokenGranter {

    public static final String GRANT_TYPE = EnumTokenGranter.PASSWORD_TOKEN_GRANTER.getMsg();

    @Autowired
    private IUserService userService;

    @Override
    public UserInfo grant(TokenParameter tokenParameter) {
        String tenantId = tokenParameter.getArgs().getStr("tenantId");
        String account = tokenParameter.getArgs().getStr("account");
        String password = tokenParameter.getArgs().getStr("password");
        UserInfo userInfo = null;
        if (StringUtils.isNoneBlank(account, password)) {
            // 获取用户类型
            String userType = tokenParameter.getArgs().getStr("userType");
            // 根据不同用户类型调用对应的接口返回数据，用户可自行拓展
            if (userType.equals(EnumUser.WEB.getName())) {
                userInfo = userService.userInfo(tenantId, account, DigestUtil.encrypt(password));
            } else if (userType.equals(EnumUser.APP.getName())) {
                userInfo = userService.userInfo(tenantId, account, DigestUtil.encrypt(password));
            } else {
                userInfo = userService.userInfo(tenantId, account, DigestUtil.encrypt(password));
            }
        }
        return userInfo;
    }

}